package com.zjitc.service.imp;

import com.zjitc.common.ServerResponse;
import com.zjitc.dao.OrderDao;
import com.zjitc.model.Order;
import com.zjitc.model.OrderItem;
import com.zjitc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.Date;

/**
 * Create by IntelliJ IDEA
 *
 * @author: MaoYongQi
 * @date-Time: 2018/3/29 9:13
 * @description:
 */
@Service
public class OrderServiceImpl implements OrderService {

  /**
   * 退款
   */
  private static final Integer STATUS_REFUND = 0;
  /**
   * 未付款
   */
  private static final Integer STATUS_ARREARAGE = 10;
  /**
   * 已付款
   */
  private static final Integer STATUS_ACCOUNT_PAID = 20;
  /**
   * 已发货
   */
  private static final Integer STATUS_SHIPPED = 40;
  /**
   * 交易成功
   */
  private static final Integer STATUS_SUCCESS = 50;
  /**
   * 交易关闭
   */
  private static final Integer STATUS_FACK = 60;

  @Autowired
  private OrderDao dao;

  @Override
  public ServerResponse<Order> create(String shippingId, Order order) {
    try {
      Date date = new Date();
      order.setOrderNo(new BigInteger("14141134141231"));
      order.setStatus(STATUS_ARREARAGE);
      order.setShippingId(Integer.valueOf(shippingId));
      order.setCreateTime(date);
      dao.addOrder(order);
      for (OrderItem item : order.getOrderItemVoList()) {
        item.setCreateTime(date);
        dao.addOrderItem(order.getOrderItemVoList());
      }

      order.setUserId(null);
      return ServerResponse.createSuccess(order);
    } catch (Exception e) {
      e.printStackTrace();
      return ServerResponse.createError("创建订单失败");
    }
  }
}
